package com.huantai.common.util.http;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * 简单封装的HttpClient
 * 
 * @author Ma Chao
 * @version 2018年4月18日 上午11:30:54
 */
@SuppressWarnings("deprecation")
public class HttpClient {
    /** 默认请求超时（单位：毫秒） */
    private static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
    /** 默认读取超时（单位：毫秒） */
    private static final int DEFAULT_SO_TIMEOUT = 90000;

    /** HttpClient */
    private org.apache.http.client.HttpClient httpClient;
    /** 请求超时时间 */
    private int connectionTimeout;
    /** 读取超时时间 */
    private int soTimeout;

    /**
     * 默认构造函数
     */
    public HttpClient() {
        this.connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
        this.soTimeout = DEFAULT_SO_TIMEOUT;
        this.httpClient = new DefaultHttpClient();// NOSONAR
        this.httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, // NOSONAR
                connectionTimeout);
        this.httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);// NOSONAR
    }

    /**
     * 构造函数
     * 
     * @param connectionTimeout 请求超时时间
     * @param soTimeout 读取超时时间
     */
    public HttpClient(int connectionTimeout, int soTimeout) {
        this.connectionTimeout = connectionTimeout;
        this.soTimeout = soTimeout;
        this.httpClient = new DefaultHttpClient();// NOSONAR
        this.httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, // NOSONAR
                connectionTimeout);
        this.httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);// NOSONAR

    }

    /**
     * 开启SSL
     * 
     * @Title: enableSSL
     * @Description: copy from baofoo demo
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public void enableSSL() throws NoSuchAlgorithmException, KeyManagementException {

        SSLContext ctx = SSLContext.getInstance("TLS");// NOSONAR
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType)// NOSONAR
                    throws CertificateException {}

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType)// NOSONAR
                    throws CertificateException {}

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;// NOSONAR
            }
        };
        ctx.init(null, new TrustManager[] {tm}, null);
        SSLSocketFactory ssf = // NOSONAR
                new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);// NOSONAR
        ClientConnectionManager ccm = httpClient.getConnectionManager();// NOSONAR
        SchemeRegistry sr = ccm.getSchemeRegistry();// NOSONAR
        sr.register(new Scheme("https", 443, ssf));// NOSONAR

    }

    /**
     * 获取：HttpClient
     */

    public org.apache.http.client.HttpClient getHttpClient() {
        return httpClient;
    }

    /**
     * 获取：请求超时时间
     */

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    /**
     * 设置：请求超时时间
     */
    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
        this.httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, // NOSONAR
                connectionTimeout);
    }

    /**
     * 获取：读取超时时间
     */
    public int getSoTimeout() {
        return soTimeout;
    }

    /**
     * 设置：读取超时时间
     */
    public void setSoTimeout(int soTimeout) {
        this.soTimeout = soTimeout;
        this.httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);// NOSONAR
    }
}
